#include<bits/stdc++.h>
using namespace std;
const int N=1e7+5,mod=1e9+7;
long long n,m,a[N],dp[N],top,top1,w[N],qwq[N],top2,top3,b,ans,mx;
struct node{
	long long id,z; 
}p[N];
int main()
{
	freopen("knight.in","r",stdin); 
	freopen("knight.out","w",stdout); 
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;++i)
		cin>>a[i],dp[i]=1e11;
	if(n<=500)
	{dp[1]=a[1];
	for(int i=1;i<=n;++i)
	{
		for(int j=max(0ll,i-m);j<i;++j)
		{
			mx=0;
			for(int z=j+1;z<=i;++z)
				mx=max(mx,a[z]);
			dp[i]=min(dp[j]+mx,dp[i]);
		}
	}
	b=1;
	for(int i=n;i>=1;--i)
	{
		dp[i]%=mod;
		ans=(ans+(b*dp[i])%mod)%mod;
		b=(b*23)%mod;
	}	
	cout<<ans;
	}
	else{
	dp[1]=a[1];
	++top,++top1,w[top]=0;
	top2=top3=1;
	for(int i=1;i<=n;++i)
	{
		while(top<=top1&&w[top]<i-m+1) qwq[w[top]]=1,++top;
//		cout<<top<<" "<<top1<<" "<<w[top]<<" ";
		if(p[top2].id-p[top2-1].id<=m&&p[top2-1].id)p[top2].z=dp[p[top2-1].id];
		else p[top2].z=dp[max(0ll,i-m)];
//		cout<<a[p[top2+1].id];
		while(top<=top1&&a[i]>=a[w[top1]]) qwq[w[top1]]=1,--top1;
		++top1,w[top1]=i;
		while(top2<=top3&&qwq[p[top3].id])--top3;
		++top3,p[top3].id=i;
		p[top3].z=dp[p[top3-1].id];
		if(p[top2].id-p[top2-1].id<=m&&p[top2-1].id)p[top2].z=dp[p[top2-1].id];
		else p[top2].z=dp[max(0ll,i-m)];
//		cout<<p[top3-1].id<<" ";
		while(top2<=top3&&(qwq[p[top2].id])) ++top2;
		while(top2<top3&&p[top2].z+a[p[top2].id]>p[top2+1].z+a[p[top2+1].id]&&p[top2+1].z) ++top2;
		if(p[top2].id-p[top2-1].id<m&&p[top2-1].id)p[top2].z=dp[p[top2-1].id];
		else p[top2].z=dp[max(0ll,i-m)];
//		while(top2<top3&&a[p[top2].id]+p[top2].z>) ++top2;
		cout<<p[top2].id-p[top2-1].id<<" "<<top2<<" "<<p[top2].id<<" "<<p[top2].z<<" "<<top3<<'\n';
		dp[i]=a[p[top2].id]+p[top2].z;
		p[top3].z=dp[p[top3-1].id];
		if(p[top2].id-p[top2-1].id<=m&&p[top2-1].id)p[top2].z=dp[p[top2-1].id];
		else p[top2].z=dp[max(0ll,i-m)];
	}
	b=1;
	for(int i=n;i>=1;--i)
	{
		dp[i]%=mod;
		ans=(ans+(b*dp[i])%mod)%mod;
		b=(b*23)%mod;
	}	
	cout<<ans;
	}	
	return 0;
}
